package com.tjbank.cssys.web.controller;

import com.github.pagehelper.PageInfo;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.domain.CkOrderRecoveryData;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.service.CkOrderRecoveryDataService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
@Api(tags = "CkOrderRecoveryDataController", description = "催记查询管理")
@RestController
@RequestMapping("/recovery")
public class CkOrderRecoveryDataController {
    @Autowired
    private CkOrderRecoveryDataService ckOrderRecoveryDataService;

    @Autowired
    private CkLogService ckLogService;

    private static final Logger logger = LoggerFactory.getLogger(CkOrderRecoveryDataController.class);

    @ApiOperation("获取催记列表")
    @PostMapping("/list")
    public CommonResult<PageInfo> getRecoveryList(@RequestBody Map<String, String> map, HttpServletRequest request) {
        PageInfo<CkOrderRecoveryData> pageInfo = ckOrderRecoveryDataService.selectCkOrderRecoverlist(map, request);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams("");
        ckLog.setOperationType("获取催记列表");

        if (pageInfo != null) {
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(pageInfo);
        } else {
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("错误");
        }
    }

    @ApiOperation("获取短信催记记录")
    @PostMapping("/message/list")
    public CommonResult<PageInfo> getMessageList(@RequestBody Map<String, String> map, HttpServletRequest request) {

        map.put("notificationMethod", "1");//1为短信

        PageInfo<CkOrderRecoveryData> pageInfo = ckOrderRecoveryDataService.selectCkOrderRecoverlist(map, request);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams("");
        ckLog.setOperationType("获取短信催记记录");

        if (pageInfo != null) {
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(pageInfo);
        } else {
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("错误");
        }

    }

    @ApiOperation("获取催记详情")
    @PostMapping("/getCkOrderInfo")
    public CommonResult<Map> getCkOrderInfoById(@RequestBody Map<String, String> map, HttpServletRequest request) {
        Map result = ckOrderRecoveryDataService.getCkOrderrecoverById(map);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams("");
        ckLog.setOperationType("获取催记详情");

        if (ckOrderRecoveryDataService != null) {
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(result);
        } else {
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("错误");
        }
    }
}
